package com.yykj.common.util;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by chendg
 * on 2018/5/7 0007.
 */
public class HttpUtil {

    public static String post(String url, JSONObject jsonObject) throws UnirestException {

        HttpResponse<String> response = Unirest.post(url)
                .header("accept", "application/json")
                .header("content-type", "application/json")
                .body(jsonObject).asString();

        return response.getBody();
    }

    public static String post(String url, JSONObject jsonObject,Map<String,String> headers) throws UnirestException {


        HttpResponse<String> response = Unirest.post(url)
                .headers(headers)
                .body(jsonObject).asString();

        return response.getBody();
    }




    public static String post(String url) throws UnirestException {
        HttpResponse<String> response = Unirest.post(url).asString();
        return response.getBody();
    }

    public static String post(String url, String... param) throws UnirestException {
        Map<String, Object> map = new HashMap<>();
        int len = param.length;
        for (int i = 0; i < len; i += 2) {
            if (i + 1 < len) {
                map.put(param[i], param[i + 1]);
            }
        }
        return post(url, map);
    }

    public static String post(String url, Map<String, Object> param) throws UnirestException {
        return post(url,param,null);
    }


    public static String post(String url, Map<String, Object> param,Map<String,String> headers) throws UnirestException {
        HttpResponse<String> response;
        if (null == param) {
            if(headers == null){
                response = Unirest.post(url).asString();
            }else{
                response = Unirest.post(url).headers(headers).asString();
            }
        } else {
            if(headers == null){
                response = Unirest.post(url).fields(param).asString();
            }else{
                response = Unirest.post(url).headers(headers).fields(param).asString();
            }
        }
        return response.getBody();
    }

    public static String get(String url, String... param) throws UnirestException {
        Map<String, Object> map = new HashMap<>();
        int len = param.length;
        for (int i = 0; i < len; i += 2) {
            if (i + 1 < len) {
                map.put(param[i], param[i + 1]);
            }
        }
        return get(url, map);
    }

    public static String get(String url, Map<String, Object> param) throws UnirestException {
        HttpResponse<String> response;
        if (null == param) {
            response = Unirest.get(url).asString();
        } else {
            response = Unirest.get(url).queryString(param).asString();
        }
        return response.getBody();
    }

    public static String get(String url) throws UnirestException {
        HttpResponse<String> response = Unirest.get(url).asString();
        return response.getBody();
    }

    public static InputStream getRawBody(String url) throws UnirestException {
        HttpResponse<InputStream> response = Unirest.get(url).asBinary();
        return response.getRawBody();
    }

}
